package org.WeekCompetition;

import java.util.ArrayList;
import java.util.List;

import java.util.ArrayList;
import java.util.List;

class Solution {
	public List<Integer> findNotSuspiciousMethods(int n, int k, int[][] invocations) {
		List<Integer> result = new ArrayList<>();
		boolean[] suspicious = new boolean[n];
		if (k < 0 || k >= n) {
			for (int i = 0; i < n; i++) {
				result.add(i);
			}
			return result;
		}
		// 标记可疑方法
		suspicious[k] = true;
		List<Integer>[] adjList = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			adjList[i] = new ArrayList<>();
		}
		for (int[] invocation : invocations) {
			adjList[invocation[0]].add(invocation[1]);
			if (suspicious[invocation[1]]) {
				suspicious[invocation[0]] = true;
			}
		}
		boolean canRemoveAllSuspicious = true;
		for (int i = 0; i < n; i++) {
			if (suspicious[i]) {
				for (int calledBy : adjList[i]) {
					if (!suspicious[calledBy]) {
						canRemoveAllSuspicious = false;
						break;
					}
				}
			}
		}
		if (canRemoveAllSuspicious) {
			for (int i = 0; i < n; i++) {
				if (!suspicious[i]) {
					result.add(i);
				}
			}
		} else {
			for (int i = 0; i < n; i++) {
				if (!suspicious[i]) {
					result.add(i);
				}
			}
		}
		return result;
	}


	public static void main(String[] args) {
		Solution solution = new Solution();
		// 示例 2
		int[][] invocations2 = {{1, 2}, {0, 2}, {0, 1}, {3, 4}};
		List<Integer> result2 = solution.findNotSuspiciousMethods(5, 0, invocations2);
		System.out.println(result2);
	}
}

